//
//  ZCNewFeatureController.swift
//  weibo
//
//  Created by 张城 on 16/7/22.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit


private let NewfeatureViewCellId = "cellID"

class ZCNewFeatureController: UIViewController {
    
    lazy var collectView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSizeMake(SCREENW, SCREENH)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        let collview = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        collview.dataSource = self
        collview.delegate = self
        collview.registerClass(ZCNewFeatureCell.self, forCellWithReuseIdentifier: NewfeatureViewCellId)
        collview.pagingEnabled = true
        collview.showsHorizontalScrollIndicator = false
        collview.bounces = false
        
        return collview
    }()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(collectView)
        collectView.snp_makeConstraints { (make) in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
      

    }
}


extension ZCNewFeatureController : UICollectionViewDataSource ,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewfeatureViewCellId, forIndexPath: indexPath) as? ZCNewFeatureCell
        
        cell?.imgIndex = indexPath.item
        return cell!
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let index =  Int(scrollView.contentOffset.x / SCREENW)
        
        
        if index == 3 {
            
            let cell = self.collectView.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as? ZCNewFeatureCell
            
            
            cell?.startAnim()
            
        }
        
        
    }
    
    
}


class ZCNewFeatureCell: UICollectionViewCell {
    
    private lazy var bgImgView: UIImageView = UIImageView()
    private lazy var startButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        button.setTitle("欢迎体验", forState: .Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        button.sizeToFit()
        button.addTarget(self, action: #selector(ZCNewFeatureCell.clickButton), forControlEvents: .TouchUpInside)
        return button
    }()
    
    func clickButton() {
       
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootNoti, object: "newFeature")
        
    }
    
    var imgIndex: Int = 0 {
        didSet{
         bgImgView.image = UIImage(named: "new_feature_\(imgIndex + 1)")
         startButton.hidden = true
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        contentView.backgroundColor = RandomColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() {
        contentView.addSubview(bgImgView)
        contentView.addSubview(startButton)
        bgImgView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        startButton.snp_makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
        startButton.hidden = true
    }
    
    func startAnim() {
        startButton.hidden = false
        startButton.userInteractionEnabled = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { 
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) in
                self.startButton.userInteractionEnabled = true
        }
        
        
    }
}






















